package com.travel.server.manager.impl.command.data;

import com.travel.common.CommonConstant;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.data.QueryOperateSaleTargetRequest;
import com.travel.server.manager.dto.response.data.QueryOperateSaleTargetResponse;
import com.travel.server.manager.dto.response.data.QueryOperateSaleTargetResponse.TrendItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.impl.dal.data.UserTargetDal;
import com.travel.server.manager.impl.sal.OrderDataSal;
import com.travel.server.manager.impl.vo.DataDateVo;
import com.travel.server.manager.model.UserInfo;
import com.travel.server.manager.model.dto.data.WholeAllDto;
import com.travel.server.manager.model.dto.data.WholeTrendDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/14 19:12
 * @Desc:
 */
@Component
@Slf4j
public class QueryOperateSaleTargetCommand extends AbstractCommand<QueryOperateSaleTargetRequest, QueryOperateSaleTargetResponse> {

    @Autowired
    private UserTargetDal userTargetDal;

    @Autowired
    private OrderDal orderDal;

    @Override
    protected void checkInput(QueryOperateSaleTargetRequest request) {
        checkTimeInput(request);
        if(request.getType() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryOperateSaleTargetResponse handle(QueryOperateSaleTargetRequest request) {
        DataDateVo dateVo = OrderDataSal.getDate(request.getType(), request);
        Date startTime = dateVo.getStartTime();
        Date endTime = dateVo.getEndTime();
        Date lastTime = dateVo.getEndTime();

        UserInfo userInfo = getUserInfo();

        QueryOperateSaleTargetResponse response = makeResponse(userInfo.getId(), request.getType(), lastTime, startTime, endTime);
        response.setTrendList(makeTrend(lastTime, startTime, endTime));

        return response;
    }

    private List<TrendItem> makeTrend(Date lastTime, Date startTime, Date endTime){
        List<WholeTrendDto> list = orderDal.selectWholeTrend(lastTime, endTime, null);
        Map<String, Integer> map = makeMap(list);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        Calendar lastCalendar = Calendar.getInstance();
        lastCalendar.setTime(lastTime);
        List<TrendItem> trendList = new ArrayList<>();
        while(calendar.getTime().before(endTime) || calendar.getTime().equals(endTime)){
            String day = DateUtil.YYYY_MM_DD.format(calendar.getTime());
            String lastDay = DateUtil.YYYY_MM_DD.format(lastCalendar.getTime());
            TrendItem item = new TrendItem();
            item.setDay(calendar.getTime());
            Integer current = map.get(day);
            current = current == null ? 0 : current;
            Integer last = map.get(lastDay);
            last = last == null ? 0 : last;
            item.setLastDay(lastCalendar.getTime());
            item.setPay(current + "");
            item.setAdd(OrderDataSal.getProportion(current, last));
            trendList.add(item);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            lastCalendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return trendList;
    }

    private Map<String, Integer> makeMap(List<WholeTrendDto> list){
        Map<String, Integer> map = new HashMap<>();
        if(!CollectionUtil.isEmpty(list)){
            for(WholeTrendDto dto : list){
                map.put(dto.getDate(), dto.getAmount());
            }
        }
        return map;
    }

    private QueryOperateSaleTargetResponse makeResponse(Integer userId, Integer type, Date lastTime, Date startTime, Date endTime){
        Integer dateType;
        if (CommonConstant.MONTH.equals(type)){
            dateType = 2;
        }else{
            dateType = 1;
        }
        Integer totalTarget = userTargetDal.selectSumTarget(userId, startTime, endTime, dateType);

        WholeAllDto currentOrder = CollectionUtil.getFirst(orderDal.selectAllData(startTime, endTime));
        Date lastEndTime = DateUtil.setDayEnd(startTime);
        lastEndTime = DateUtil.getIndexDay(lastEndTime, Calendar.DAY_OF_MONTH, -1);
        WholeAllDto yesterdayOrder = CollectionUtil.getFirst(orderDal.selectAllData(lastTime, lastEndTime));

        QueryOperateSaleTargetResponse response = new QueryOperateSaleTargetResponse();
        response.setTarget(totalTarget == null ? null : totalTarget + "");
        response.setToday(currentOrder.getPayNum() + "");
        response.setTotal(currentOrder.getPayNum() + "");
        response.setYesterday(yesterdayOrder.getPayNum() + "");
        return response;
    }
}
